<script setup>
import * as api from "@/api/v-sys-service/sys-manage/dict-type/index.js";
import dictTypeFormDialog from "./dict-type-form-dialog.vue"
import dictItemsTable from "./dict-items-table/index.vue"
import Consts from "@/const/index.js";
/**
 * 组件挂载后
 */
onMounted(() => {
  init()
})

/**
 * 列表数据
 */
const tableData = ref([])

/**
 * loading状态
 */
const loading = ref({
  tableLoading: false,// 列表loading
  formBtnLoading: false,// 保存按钮loading
})

/**
 * 弹窗显隐
 */
const dialog = ref({
  showFormDialog: false, //表单弹窗
  showDictItemsDialog: false, //列表显影
})

/**
 * 弹窗标题
 */
const title = ref("")

const rowData = ref({})

/**
 * 计算表格属性
 */
const tableInfo = computed(()=>{
  const tableProps = {
    data: tableData.value,
    loading: loading.value.tableLoading,
    checkConfig: { highlight: true },
    showSelectCount: false,
    showExportBtn: false,
  }
  return { ...tableProps }
})

/**
 * 分页参数
 */
const pageQuery = reactive({
  page: 1,
  limit: 50,
  total: 0,
  orders: [],
  filters: [],
  condition: {
    key: "",
  },
})

/**
 * 组件初始化调用函数
 */
const init = () => {
  init_PageQuery()
}

/**
 * 分页查询
 */
const init_PageQuery = async () => {
  loading.value.tableLoading = true
  const { code, data } = await api.list(pageQuery)
  if (code === 200) {
    loading.value.tableLoading = false
    tableData.value = data.records
    pageQuery.total = data.total
  }
}

/**
 * 查看字典项列表
 */
const showDictItemsDialog = (row) => {
  if (row && row.id) rowData.value = row
  else rowData.value = row || {}
  title.value = "" + row.name + " 字典数据"
  dialog.value.showDictItemsDialog = true
}

/**
 * form表单
 */
const showFormDialog = ($event,row) => {
  if (row && row.id) rowData.value = row
  else rowData.value = row || {}
  dialog.value.showFormDialog = true
}

/**
 * 删除
 */
const showRemoveDialog = ($event,row) => {
  ElMessageBox.confirm('', '确定删除吗',
      {
        closeOnClickModal: false,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            instance.confirmButtonText = '拼命加载中...'
            api.remove(row.id).then(({code})=>{
              instance.confirmButtonLoading = false
              if (code === 200){
                ElMessage({
                  message: '删除成功',
                  type: 'success',
                })
                init_PageQuery()
              }
            })
            done()
          } else {
            done()
          }
        }
      }
  )
}

/**
 * 组件对外提供可访问数据
 */
defineExpose({
  showFormDialog,
  showRemoveDialog,
})

</script>

<template>
  <div>
    <div class="dict-box flex flex-col" v-if="!dialog.showDictItemsDialog">
      <div class="search flex align-center">
        <label class="label">{{'\u2003'}}关键字{{'\u2003'}}</label>
        <el-input
            v-model="pageQuery.condition.key"
            style="width: 180px;" placeholder="请输入关键字"
            @keyup.enter="init_PageQuery" @clear="init_PageQuery" clearable
        />
        <div class="el-button" style="display: none"></div>
        <el-button type="primary" @click="init_PageQuery">
          <template #icon><i-ep-search /></template>搜索
        </el-button>
        <el-button type="success" @click="showFormDialog">
          <template #icon><i-ep-plus /></template>新增
        </el-button>
      </div>
      <v-table
          v-model="pageQuery"
          :table-info="tableInfo"
          :column-info="api.columnInfo"
          @query="init_PageQuery"
      >

        <template #name="{row}">
          <el-tag @click="showDictItemsDialog(row)"><v-svg-icon icon-class="dict" />{{ row.name }}</el-tag>
        </template>
        <template #status="{row}">
          <el-tag :type="Consts.DICT_STATUS_TYPE[row.status].type">{{ Consts.DICT_STATUS_TYPE[row.status].label }}</el-tag>
        </template>
        <template #operate="{row}">
          <el-button type="primary" :text="true" @click="showFormDialog($event,row)">
            <template #icon><i-ep-edit /></template>编辑
          </el-button>
          <el-button type="primary" :text="true" @click="showRemoveDialog($event,row)">
            <template #icon><i-ep-delete /></template>删除
          </el-button>
        </template>
      </v-table>
      <dict-type-form-dialog v-if="dialog.showFormDialog" :title="title" @query="init_PageQuery" v-model="dialog.showFormDialog" :row-data="rowData"/>
    </div>
    <dict-items-table v-else v-model="dialog.showDictItemsDialog" @query="init_PageQuery" :row-data="rowData"></dict-items-table>
  </div>
</template>

<style scoped lang="scss">
.dict-box {
  padding: 10px;
  .container {
    :deep(.el-tag) {
      cursor: pointer;
    }
    :deep(.el-button) {
      padding: 0;
    }
  }
}
</style>
